package com.blue.http.util;

import com.blue.http.config.ErrorConfig;

/**
 * HTTP服务异常
 * 
 * @author zhengj
 * @since 1.0 2016年11月13日
 */
public class HttpException extends RuntimeException
{
	private static final long serialVersionUID = 1L;
	
	private int code;
	private String message;
	private String extMessage;

	public HttpException(String extMessage)
	{
		this(ErrorConfig.BUSINESS, ErrorConfig.getInstance().get(ErrorConfig.BUSINESS), extMessage);
	}

	public HttpException(int code, String message)
	{
		this(code, message, null);
	}

	public HttpException(int code, String message, String extMessage)
	{
		super(message + (extMessage != null && !extMessage.isEmpty() ? ":" + extMessage : ""), null, true, false);
		this.code = code;
		this.message = message;
		this.extMessage = extMessage;
	}

	public int getCode()
	{
		return code;
	}

	@Override
	public String getMessage()
	{
		return message;
	}

	public String getExtMessage()
	{
		return extMessage;
	}

	public String getFullMessage()
	{
		String msg = message;
		if (extMessage != null && !extMessage.isEmpty())
			msg += ":" + extMessage;
		return msg;
	}

	@Override
	public String toString()
	{
		return String.format("[%d]%s", code, this.getFullMessage());
	}
	
}
